// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem3.h"

using namespace std;
// @before-stub-for-debug-end

/*
 * @lc app=leetcode.cn id=3 lang=cpp
 *
 * [3] 无重复字符的最长子串
 */

// @lc code=start
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<char, int> need, window;

        int left = 0;
        int right = 0;
        int res = 0;
        while(right<s.size())
        {
            //c是移入窗口的字符
            char c = s[right];

            //右移窗口
            right++;

            //进行窗口内数据的更新
            window[c]++;

            //判断左侧窗口是否需要收缩
            while(window[c] > 1)
            {
                char d = s[left];
                left++;
                //进行窗口内数据的更新
                window[d]--;
            }
            res = max(res, right - left);
        }

        return res;
    }


};
// @lc code=end

